package utils;

import play.Play;

/**
 * Created by IntelliJ IDEA.
 * User: Cyril
 * Date: 13.08.11
 * Time: 1:25
 * To change this template use File | Settings | File Templates.
 */
public class DefaultPagerLayout {

    public void doLoyout(Pager pager) {
        int number = Integer.valueOf( Play.configuration.getProperty("pager.show.pages", "3") );
        int cur = pager.getCurrentPage();
        int min = Math.max(1, cur - number / 2);
        int max = Math.min(pager.getNumPages(), cur + number / 2);

        if (min == 1)
            max = Math.min(min + number - 1, pager.getNumPages());
        else if (max == pager.getNumPages())
            min = Math.max(1, max - number + 1);
        if (min > 1)
            pager.add(pager.createPage(1, "<<"));
        if (cur > 1)
            pager.add(pager.createPage(cur - 1, "<"));

        for (int i = min; i <= max; i++)
            pager.add(pager.createPage(i, Integer.toString(i)));

        if (cur < pager.getNumPages())
            pager.add(pager.createPage(cur + 1, ">"));
        if (max < pager.getNumPages())
            pager.add(pager.createPage(pager.getNumPages(), ">>"));
    }

}
